package com.bw.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bw.common.entity.SimpleVideoEntity
import com.bw.home.R
import com.bw.home.databinding.ItemVideoLayoutBinding
import com.bw.image.glide.GlideImgLoader
import com.luck.picture.lib.utils.ToastUtils

class HomePlayAdapter(private val list: List<SimpleVideoEntity>):RecyclerView.Adapter<HomePlayAdapter.ItemViewHolder>() {

    var currentIndex:Boolean = false
    var checked:Boolean = false

    private var itemClickListener: OnItemClickListener ?= null

    fun setOnItemClickListener(l: OnItemClickListener){
        itemClickListener = l
    }

    interface OnItemClickListener{
        fun onItemClick(v: View,position: Int)
    }

    inner class ItemViewHolder(val dataBinding: ItemVideoLayoutBinding):RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        holder.dataBinding.gsy.apply {
            setUp(entity.videopath,true,"")
        }
        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context,entity.avatar_url,holder.dataBinding.ivUserIcon)
        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context,entity.avatar_url,holder.dataBinding.ivRotation)
        //holder.dataBinding.tvTitle.setText(entity.typename)
        holder.dataBinding.tvAuthname.setText(entity.name)
        holder.dataBinding.tvRun.setText(entity.description)
        holder.dataBinding.tvLove.setText(entity.id.toString())
        holder.dataBinding.tvStore.setText(entity.id.toString())
        holder.dataBinding.tvTitle.setOnClickListener {
            itemClickListener?.onItemClick(it,position)
        }
        holder.dataBinding.ivShare.setOnClickListener {
            itemClickListener?.onItemClick(it,position)
        }
        holder.dataBinding.ivLove.setOnClickListener {
            checked = !checked
            if(checked){
                val red =
                    ContextCompat.getDrawable(holder.dataBinding.root.context, R.drawable.ic_xihuanred)
                holder.dataBinding.ivLove.setImageDrawable(red)
                ToastUtils.showToast(holder.dataBinding.root.context,"点赞成功")
                holder.dataBinding.tvLove.setText("${entity.id+1}")
            }else{
                val normal =
                    ContextCompat.getDrawable(holder.dataBinding.root.context, R.drawable.ic_xihuan)
                holder.dataBinding.ivLove.setImageDrawable(normal)
                ToastUtils.showToast(holder.dataBinding.root.context,"取消点赞")
                holder.dataBinding.tvLove.setText("${entity.id}")
            }
        }

        holder.dataBinding.ivStore.setOnClickListener {
            currentIndex = !currentIndex
            if(currentIndex){
                val red =
                    ContextCompat.getDrawable(holder.dataBinding.root.context, R.drawable.ic_shoucangyellow)
                holder.dataBinding.ivStore.setImageDrawable(red)
                ToastUtils.showToast(holder.dataBinding.root.context,"收藏成功")
                holder.dataBinding.tvStore.setText("${entity.id+1}")
            }else{
                val normal =
                    ContextCompat.getDrawable(holder.dataBinding.root.context, R.drawable.ic_shoucang)
                holder.dataBinding.ivStore.setImageDrawable(normal)
                ToastUtils.showToast(holder.dataBinding.root.context,"取消收藏")
                holder.dataBinding.tvStore.setText("${entity.id}")
            }
        }
    }
}